﻿using TK.Common.AspNetCore;
using TK.Common.Authorization;
namespace TK.Common
{
    /// <summary>
    /// ServiceCollectionExtensions
    /// </summary>
    public static class TKServiceCollectionExtensions
    {
        /// <summary>
        /// AddTKCommonService,常用服务注入
        /// </summary>
        public static IServiceCollection AddTKCommonService<TProgram>(this IServiceCollection services, IConfiguration configuration) where TProgram : class
        {
            //mvc controller
            services.AddTKMvc();
            //注册http请求服务
            services.AddTKHttpClient();
            //添加Mapper支持
            services.AddTKMapsterMapper();
            //验证
            services.AddTKAuthentication(configuration);
            //缓存
            services.AddTKEasyCaching();
            //分布式事务
            services.AddTKCap();
            //freesql orm
            services.AddFreeSqlOrm(builder =>
            {

            });

            return services;
        }

        /// <summary>
        /// UseTKCommonFramework,常用组件注入
        /// </summary>
        public static IApplicationBuilder UseTKCommonFramework<TProgram>(this IApplicationBuilder app, IHostApplicationLifetime lifetime) where TProgram : class
        {
            app.UseHealthMiddleware();
            app.UseCors();
            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints => endpoints.MapControllers());

            return app;
        }
    }
}